package com.yunze.system.service.impl.yunze.card;

import com.alibaba.fastjson.JSON;
import com.yunze.common.core.domain.entity.SysDictData;
import com.yunze.common.core.domain.entity.SysUser;
import com.yunze.common.mapper.mysql.card.ApplyUseCardMapper;
import com.yunze.common.utils.yunze.PageUtil;
import com.yunze.common.utils.yunze.VeDate;
import com.yunze.system.mapper.mysql.SysDeptMapper;
import com.yunze.system.mapper.mysql.SysDictDataMapper;
import com.yunze.system.service.yunze.card.IApplyUseCardService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class ApplyUseCardServiceImpl implements IApplyUseCardService {

    @Resource
    private ApplyUseCardMapper applyUseCardMapper;
    @Resource
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private SysDictDataMapper dictDataMapper;
    @Autowired
    private SysDeptMapper deptMapper;

    @Override
    public Map<String, Object> getList(Map map) {
        Map<String, Object> omp = new HashMap<String, Object>();
        Integer currenPage = map.get("pageNum") != null ? Integer.parseInt(map.get("pageNum").toString()) : 0;
        Integer pageSize = map.get("pageSize") != null ? Integer.parseInt(map.get("pageSize").toString()) : 10;
        Integer rowCount = applyUseCardMapper.selMapCount(map);
        rowCount = rowCount != null ? rowCount : 0;
        PageUtil pu = new PageUtil(rowCount, currenPage, pageSize);//初始化分页工具类
        //数据打包
        map.put("StarRow", pu.getStarRow());
        map.put("PageSize", pu.getPageSize());
        omp.put("Pu", pu);
        omp.put("Data", applyUseCardMapper.selMap(map));
        return omp;
    }

    @Override
    public boolean upd(Map map) {
        return applyUseCardMapper.upd(map)>0;
    }

    @Override
    public boolean add(Map map) {
        map.put("a_no", VeDate.getNo(6));
        return applyUseCardMapper.save(map)>0;
    }

    @Override
    public Map<String, Object> find(Map map) {
        Map<String, Object> rMap = applyUseCardMapper.find(map);
        if(rMap!=null){
              if(rMap.get("u_name")!=null){
                  String u_name = rMap.get("u_name").toString();
                  u_name = u_name.substring(0,1)+"*";
                  rMap.put("u_name",u_name);
              }
            if(rMap.get("id_number")!=null){
                String id_number = rMap.get("id_number").toString();
                int count = id_number.length();
                int sIndex = 3;
                int eIndex =count>7?7:5;
                if(count>16){
                    sIndex = 4;
                    eIndex = 14 ;
                }
                id_number = addMosaic(id_number, sIndex, eIndex);
                rMap.put("id_number",id_number);
            }

            if(rMap.get("telephone")!=null){
                String telephone = rMap.get("telephone").toString();
                telephone = addMosaic(telephone, 3, 7);
                rMap.put("telephone",telephone);
            }
            if(rMap.get("iccid")!=null){
                String iccid = rMap.get("iccid").toString();
                int count = iccid.length();
                int sIndex = 2;
                int eIndex = count>14?14:5;
                if(count>18){
                    eIndex = 18 ;
                }
                iccid = addMosaic(iccid, sIndex, eIndex);
                rMap.put("iccid",iccid);
            }
            if(rMap.get("state")!=null){
                String state = rMap.get("state").toString();
                state = state.equals("0")?"审核中":state.equals("1")?"已录入号码":"未通过";
                rMap.put("state",state);
            }
        }
        return rMap;
    }

    @Override
    public boolean export(Map map, SysUser User) {
        boolean bool = false;


        String create_by = " [ " + User.getDept().getDeptName() + " ] - " + " [ " + User.getUserName() + " ] ";
        String newName = UUID.randomUUID().toString().replace("-", "") + "_ApplyCardOut";

        String agent_id = User.getDept().getDeptId().toString();
        String task_name = "-申请用卡 [导出] ";
        String SaveUrl = "/getcsv/" + newName + ".csv";

        Map<String, Object> task_map = new HashMap<String, Object>();
        task_map.put("auth", create_by);
        task_map.put("task_name", task_name);
        task_map.put("url", SaveUrl);
        task_map.put("agent_id", agent_id);
        task_map.put("type", "100");





        //获取字典信息
        List<SysDictData> applyState = dictDataMapper.selectDictDataByType("yunze_card_applyState");//卡状态
        List<Map<String, Object>> DeptArr =  deptMapper.getDeptName();

        Map<String, Object> pMap = new HashMap<>();
        pMap.put("map",map);
        pMap.put("applyState",applyState);
        pMap.put("DeptArr",DeptArr);
        pMap.put("task_map",task_map);
        pMap.put("newName",newName);
        String msg = JSON.toJSONString(pMap);

        //生产任务
        try {
            rabbitTemplate.convertAndSend("admin_exchange", "admin.applyUseCardOut.queue", msg, message -> {
                // 设置消息过期时间 time 分钟 过期
                message.getMessageProperties().setExpiration("" + (10 * 1000 * 60));
                return message;
            });
            bool = true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return bool;
    }


    public String addMosaic(String str,int sIndex,int eIndex){
        String symbol = "";
        for (int i = 0; i < eIndex-sIndex; i++) {
            symbol += "*";
        }
        return str.substring(0, sIndex) + symbol + str.substring(eIndex);
    }




}
